﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using DTO.Models;

namespace CIT.Controllers
{
    public class TuvanController : Controller
    {
        //
        // GET: /Tuvan/

        public ActionResult Index()
        {
            DataRepository dr = new DataRepository();
            MEDIA[] lstBannerTuVan = (dr.MediaProvider.getMedia("bannertuvan")).ToArray();
            return View(lstBannerTuVan);
        }

        public ActionResult tuvanitem(string id)
        {
            DataRepository dr = new DataRepository();
            MEDIA[] arr = dr.MediaProvider.getMediaTopOut(id).ToArray();
            ViewData["TypeMediaName"] = id;
            return View(arr);
        }

        public ActionResult tuvanintro(string id)
        {
            DataRepository dr = new DataRepository();
            MEDIA[] arr = dr.MediaProvider.getMediaByContainsTypeMediaName(id).ToArray();
            return View(arr);
        }

        public ActionResult xemtuvan(string id, string pageIndex)
        {
            ViewData["TypeMediaName"] = id;
            ViewData["PageIndex"] = pageIndex == null ? "1" : pageIndex;
            return View();
        }

        public ActionResult Detail(string id, string typeMediaName, string pageIndex)
        {
            ViewData["TypeMediaName"] = typeMediaName;
            ViewData["PageIndex"] = pageIndex;
            DataRepository dr = new DataRepository();
            MEDIA media = dr.MediaProvider.getMediaOut(typeMediaName, id);
            media.Content = System.Web.HttpUtility.HtmlDecode(media.Content);
            return View(media);
        }

        //id nhận một trong 2 giá trị daotao1, daotao2, daotao3, daotao4
        [HttpGet]
        public ActionResult List(string id, string pPageIndex)
        {
            //id : pTypeMediaName
            int PageSize = Common.pagesize;
            int PageIndex = Convert.ToInt32(pPageIndex);

            DataRepository dr = new DataRepository();

            MEDIA[] source;
            source = dr.MediaProvider.getMediaOut(id).ToArray();
            MEDIA[] datapaging = source.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToArray();

            int len = source.Length;

            ViewData["TotalPage"] = (len % PageSize == 0 ? len / PageSize : len / PageSize + 1).ToString();
            ViewData["PageIndex"] = PageIndex.ToString();
            ViewData["PageSize"] = PageSize.ToString();
            ViewData["TypeMediaName"] = id;
            return View(datapaging);

        }

    }
}
